package pl.edu.agh.xls2html.htmlelements;

public class HTMLHelper {
	public static final String NBSP_ELEMENT = "&nbsp;";
	private static String htmlHeaderStart = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
			+ "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"en\" lang=\"en\">\n<head>\n<title>Spreadsheet</title>\n"
			+ "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />\n"
			+ "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />\n";
	private static String cellsStyleSheetStart = "<link rel=\"stylesheet\" type=\"text/css\" href=\"cells";
	private static String cellsStyleSheetEnd = ".css\" />\n";
	private static String jsHeader = "<script type=\"text/javascript\" src=\"formula.js\"></script>";

	private static String htmlHeaderEnd = "</head>\n<body>\n";

	private static String frameset = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Frameset//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd\">\n"
			+ "<html>\n"
			+ "<frameset rows=\"10%,90%\">\n"
			+ "\t<frame src=\"menu.html\" />\n"
			+ "\t<frame name=\"content\" src=\"sheet0.html\" />\n"
			+ "</frameset>\n</html>";

	private static String htmlFooter = "</body></html>";

	private static String rowStart = "\t<tr>\n";
	private static String rowEnd = "\t</tr>\n";
	private static String tableStart = "<table cellpadding=\"0\"  cellspacing=\"0\" >\n";
	private static String tableEnd = "</table>\n";
	private static String divStart = "<div>";
	private static String divEnd = "</div>";
	private static String inputStart = "<input ";
	private static String tdStart = "\t\t<td ";
	private static String tdEnd = "</td>\n";

	private static String tagEnd = " />";
	private static String quotation = "\"";

	public static String makeRow(String content) {
		return rowStart + content + rowEnd;
	}

	public static String makeTable(String content) {
		return tableStart + content + tableEnd;
	}

	public static String makeCell(String content, String cellclass,
			int rowspan, int colspan) {
		String res = tdStart;
		if (cellclass != null && cellclass != "")
			res += "class=\"" + cellclass + quotation;
		if (colspan != 0)
			res += " colspan=\"" + colspan + quotation;
		if (rowspan != 0)
			res += " rowspan=\"" + rowspan + quotation;
		res += " >" + content + tdEnd;
		return res;
	}

	public static String makeInput(String value, String coordinates) {
		return makeInputWithAttributes(value, coordinates, null, null);
	}

	public static String makeInputReadOnly(String value, String coordinates) {
		return makeInputWithAttributes(value, coordinates, null, "readonly");
	}

	public static String makeInputHidden(String value, String coordinates) {
		return makeInputWithAttributes(value, coordinates + "_Hidden",
				"hidden", null);
	}

	public static String makeInputWithAttributes(String value,
			String coordinates, String type, String addAttribute) {
		StringBuffer input = new StringBuffer();
		input.append(inputStart + "id=\"").append(coordinates).append(
				"\" value=\"");
		input.append(value).append(quotation);

		if (type != null)
			input.append(" type=\"").append(type).append(quotation);

		if (addAttribute != null)
			input.append(" ").append(addAttribute);

		input.append(tagEnd);

		return input.toString();
	}

	public static String makeInputForFormula(String value, String coordinates) {
		return HTMLHelper.makeInputWithAttributes(value, coordinates, null,
				"onchange=\"evaluateAllFormula()\"");
	}

	public static String makeDocument(String content, int styleSheetNum,
			Boolean javascript) {
		String result = htmlHeaderStart;
		if (styleSheetNum != -1)
			result += cellsStyleSheetStart + styleSheetNum + cellsStyleSheetEnd;
		if (javascript)
			result += jsHeader;

		result += htmlHeaderEnd + content + htmlFooter;

		return result;
	}

	public static String makeFrameset() {
		return frameset;
	}

	public static String makeDiv(String content) {
		return divStart + content + divEnd;
	}

	public static String makeSheetHref(int sheetNumber) {
		return "<a href=\"sheet" + sheetNumber
				+ ".html\" target=\"content\" >Sheet " + sheetNumber
				+ "</a>&nbsp;&nbsp;&nbsp;&nbsp;";
	}

	public static String getJSValue(String coordinates) {
		return "document.getElementById('" + coordinates + "').value";
	}

	public static String getCSSForStyle(CellStyle cs, String coordinates,
			boolean isStaticMode) {
		String css = '.' + coordinates + "{\n" + "\twidth:" + cs.getCellWidth()
				+ "px;\n" + "\theight:" + cs.getCellHeight() + "px;\n"
				+ "\tbackground-color: #" + cs.getBackgroundColor() + ";\n"
				+ "\tborder-top: " + cs.getTopBorderStyle() + ";\n"
				+ "\tborder-left: " + cs.getLeftBorderStyle() + ";\n"
				+ "\tborder-right: " + cs.getRightBorderStyle() + ";\n"
				+ "\tborder-bottom: " + cs.getBottomBorderStyle() + ";\n"
				+ "\ttext-align: " + cs.getTextAlign() + ";\n" + "}\n" + "#"
				+ coordinates + "{\n" + "\tbackground-color: #"
				+ cs.getBackgroundColor() + ";\n" + "}\n";

		if (isStaticMode)
			css += "td." + coordinates + " div{" + "\twidth:"
					+ cs.getCellWidth() + "px;\n" + "\theight:"
					+ cs.getCellHeight() + "px;\n" + "}\n";

		return css;
	}
}
